//
//  UIView+GEE_fastInit.m
//  GeelyApp
//
//  Created by LJQ on 2021/4/23.
//  Copyright © 2021 Geely. All rights reserved.
//

#import "UIView+Tools_HYC_Neusoft.h"

@implementation UIView (Tools_HYC_Neusoft)

- (UIImage *)image {
    [self layoutIfNeeded];
    return [self convertViewToImage:self];
}
//使用该方法不会模糊，根据屏幕密度计算
- (UIImage *)convertViewToImage:(UIView *)view {
    
    UIImage *imageRet = [[UIImage alloc]init];
    //UIGraphicsBeginImageContextWithOptions(区域大小, 是否是非透明的, 屏幕密度);
    UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    imageRet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imageRet;
    
}

/// 修改部分圆角
/// @param radius 圆角值
/// @param corner 圆角位置
-(void)setLayers:(CGFloat)radius type:(UIRectCorner)corner {
    if (@available(iOS 11.0, *)) {
        self.layer.cornerRadius = radius;
        self.layer.maskedCorners = (CACornerMask)corner;
    } else {
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = path.CGPath;
        self.layer.mask = maskLayer;
    }
    self.layer.masksToBounds = true;
}

-(void)setLayers:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = true;
}

- colorLayer_GradientWith:(UIColor *)leftColor With:(UIColor *)rightColor{
    
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.colors = @[ (__bridge id)leftColor.CGColor, (__bridge id)rightColor.CGColor];
        gradientLayer.locations = @[@0.0, @1.0];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1.0, 0);
        gradientLayer.frame = self.frame;
        [self.layer addSublayer:gradientLayer];
    
    return self;
}

@end
